package com.kingre.et.model;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

/**
 * 高校
 * 
 * @author Jingui.Yu
 * 
 */
@Entity
@Table(name = "t_school")
public class School extends BaseObject implements Serializable {
    private static final long serialVersionUID = 9079916330215473150L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    @Column(length = 128, nullable = false)
    private String name;

    // 1=有效；0=无效
    @Column(name = "status_code", nullable = false)
    private boolean statusCode;

    // 1=已开通；0=未开通
    @Column(name = "open_consult", nullable = false)
    private boolean openConsult;

    // 10=高校；20=院系
    @Column(name = "school_type", nullable = false)
    private Integer schoolType = 10;

    @ManyToOne
    @JoinColumn(name = "gaoxiao_id", nullable = true)
    private School overSchool;

    @OneToMany(mappedBy = "overSchool")
    private Set<School> underSchool;

    @Transient
    private boolean statReport;

    public Integer getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (!(o instanceof School)) {
            return false;
        }
        final School school = (School) o;
        return this.hashCode() == school.hashCode();
    }

    @Override
    public int hashCode() {
        int result;
        result = (id != null ? id.hashCode() : 0);
        result = 29 * result + (name != null ? name.hashCode() : 0);
        result = 29 * result + (statusCode ? 1 : 0);
        return result;
    }

    @Override
    public String toString() {
        return new ToStringBuilder(this, ToStringStyle.DEFAULT_STYLE).append("id", id).append("name", name)
                .append("statusCode", statusCode).toString();
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Transient
    public boolean isStatReport() {
        return statReport;
    }

    public void setStatReport(boolean statReport) {
        this.statReport = statReport;
    }

    public boolean isStatusCode() {
        return statusCode;
    }

    public void setStatusCode(boolean statusCode) {
        this.statusCode = statusCode;
    }

    public boolean isOpenConsult() {
        return openConsult;
    }

    public void setOpenConsult(boolean openConsult) {
        this.openConsult = openConsult;
    }

    public Integer getSchoolType() {
        return schoolType;
    }

    public void setSchoolType(Integer schoolType) {
        this.schoolType = schoolType;
    }

    public School getOverSchool() {
        return overSchool;
    }

    public void setOverSchool(School overSchool) {
        this.overSchool = overSchool;
    }

    public Set<School> getUnderSchool() {
        return underSchool;
    }

    public void addUnderSchool(School underSchool) {
        if (underSchool == null)
            throw new IllegalArgumentException("Null under school!");
        if (underSchool.getOverSchool() != null)
            underSchool.getOverSchool().getUnderSchool().remove(underSchool);
        underSchool.setOverSchool(this);
        if (this.underSchool == null)
            this.underSchool = new HashSet<School>();
        this.underSchool.add(underSchool);
    }

    public void removeUnderSchool(School underSchool) {
        if (underSchool == null)
            throw new IllegalArgumentException("Null under school!");
        underSchool.setOverSchool(null);
        if (this.underSchool != null)
            this.underSchool.remove(underSchool);
    }

}
